// 
// 

const { ccclass, property, executeInEditMode } = cc._decorator;
const MINI_CAMERA_Z = 100;


@ccclass
@executeInEditMode
export default class NewClass extends cc.Component {

    @property(cc.Node)
    target: cc.Node = null;

    @property(cc.Camera)
    miniMapCamera: cc.Camera = null;

    @property(cc.Graphics)
    cameraInfo: cc.Graphics = null;


    // 
    cameraPos = cc.v3(0, 0, MINI_CAMERA_Z);
    cameraOrthoSize = 1;
    _tweens: cc.Tween[] = [];

    // onLoad () {}

    start() {

        if (!CC_EDITOR) {
            let t = cc.tween(this.target)
                .by(6, { angle: 360 })
                .repeatForever()
                .start()
            this._tweens.push(t);

            // t = cc.tween(this).set(this.cameraPos)
        }
        this.cameraOrthoSize = cc.Canvas.instance.node.height / 2;

        cc.log("  this.cameraOrthoSize ", this.cameraOrthoSize)
    }

    update(dt) {
        let { width: canvasWidth, height: canvasHeight } = cc.Canvas.instance.node;
        let deviceWidth = canvasWidth;
        let deviceHeight = canvasHeight;


        console.log("deviceWidth -> ", deviceWidth)
        console.log("deviceHeight -> ", deviceHeight)


        if (!CC_EDITOR) {
            deviceWidth = cc.game.canvas.width / cc.view._scaleX;
            deviceHeight = cc.game.canvas.height / cc.view._scaleY;
        }

        let orthoHeight = this.cameraOrthoSize;
        let orthoWidth = orthoHeight * (deviceWidth / deviceHeight);

        let rect = this.miniMapCamera.rect;
        this.cameraInfo.clear();

        // cc.log("rect -> ",rect);

        this.cameraInfo.rect((rect.x - 0.5) * deviceWidth, (rect.y - 0.5) * deviceHeight, rect.width * deviceWidth, rect.height * deviceHeight);
        this.cameraInfo.strokeColor = cc.Color.YELLOW;
        this.cameraInfo.stroke();

        this.cameraInfo.rect(this.cameraPos.x - orthoWidth, this.cameraPos.y - orthoHeight, orthoWidth * 2, orthoHeight * 2);
        this.cameraInfo.strokeColor = cc.Color.BLUE;
        this.cameraInfo.stroke();

        this.miniMapCamera.node.position = this.cameraPos;
        this.miniMapCamera.orthoSize = this.cameraOrthoSize;


    }
}
